import axios from 'axios'
import { useAuthStore } from '@/stores/auth'
import { setupInterceptors } from '@/utils/interceptors'

// 创建axios实例
const service = axios.create({
    baseURL: import.meta.env.VITE_API_BASE_URL,
    timeout: 10000
})

// 请求封装函数
export function request(config) {
    // 添加认证token
    const authStore = useAuthStore()
    if (authStore.token) {
        config.headers = config.headers || {}
        config.headers.Authorization = `Bearer ${authStore.token}`
    }

    return service(config)
}

// 设置拦截器
setupInterceptors(service)